//
//  TicketSuccessView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/3/13.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class TicketSuccessView: UIView {
    
    // 支付金额
    private let moneyLabel = UILabel(title: "", color:  UIColor.black, size: 35,align: .center)
    // 完成按钮
    private let finishlLabel = UILabel(title: "完成", color: COLOR_GREEN, size: 18,align: .center)
    // 支付成功文本
    private let successLabel = UILabel(title: "支付成功", color: COLOR_GREEN, size: 13, align: .center)
    
    public var resetMoney:String? {
        didSet {
            guard let money = resetMoney else {
                return
            }
            if money.elementsEqual("0") {
                moneyLabel.text = "变更成功，若多余金额将会原路返还"
                moneyLabel.font = UIFont.systemFont(ofSize: 14)
                return
            }
            moneyLabel.text = "¥\(money)"
        }
    }
    
    // 回调
    public var complete:(() -> Void)?
    
    override func layoutSubviews() {
        // 图片
        let img = UIImageView(image: UIImage(named: "IMG_WechatPay_24x24_"))
        let imgX = (frame.width - MARGIN35) * 0.5
        let imgY = frame.height * 0.07
        img.frame = CGRect(x: imgX, y: imgY, width: MARGIN35, height: MARGIN35)
        addSubview(img)
        // 支付成功文本
        successLabel.frame = CGRect(x: 0, y: img.frame.maxY + MARGIN10, width: frame.width, height: MARGIN20)
        addSubview(successLabel)
        // 支付金额
        let moneY = frame.height * 0.38
        moneyLabel.frame = CGRect(x: 0, y: moneY, width: frame.width, height: MARGIN40)
        addSubview(moneyLabel)
        // 完成按钮
        let finishW = frame.width * 0.5
        let finishX = (frame.width - finishW) * 0.5
        finishlLabel.layer.borderWidth = 1
        finishlLabel.layer.cornerRadius = 5
        finishlLabel.layer.borderColor = COLOR_GREEN.cgColor
        finishlLabel.frame = CGRect(x: finishX, y: frame.height * 0.75, width: finishW, height: MARGIN40)
        addSubview(finishlLabel)
        // 完成点击事件
        finishlLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickFinishLabel)))
    }
    
    @objc func clickFinishLabel() -> Void {
        if let callback = complete {
            callback()
        }
    }
}
